package me.pcqpcq.android.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>文件处理类</p>
 * <p>包含：文件或目录的读写</p>
 * @author pcqpcq
 * @since 2011.11.21
 * @version 0.1
 */
public class FileHandleUtil {
    
    public static final int SAVE_MODE_OVER_WRITE = 0;
    public static final int SAVE_MODE_APPEND = 1;

    /**
     * 保存字符串到文件
     * @param file 用于存储信息的文件
     * @param content 要存储的信息数组
     * @param mode 保存模式，SAVE_MODE_OVER_WRITE为覆盖，SAVE_MODE_APPEND为追加
     * @return 保存成功与否
     */
    public boolean writeTo(File file, String[] content, int mode) {
        
        String chLine = "\r\n"; // 换行符
        
        if (!file.getParentFile().exists())
        	file.getParentFile().mkdirs();
        
        switch (mode) {
            case FileHandleUtil.SAVE_MODE_OVER_WRITE:
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    int i = 0;
                    while (i < content.length) {
                        fos.write(content[i].getBytes());
                        fos.write(chLine.getBytes());
                        i ++;
                    }
                    fos.flush();
                    fos.close();
                    return true;
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    return false;
                }
                
            case FileHandleUtil.SAVE_MODE_APPEND:
                try {
                    FileOutputStream fos = new FileOutputStream(file, true);
                    int i = 0;
                    if (file.length() != 0) fos.write(chLine.getBytes()); // 换行，区分新旧内容
                    while (i < content.length) {
                        fos.write(content[i].getBytes());
                        fos.write(chLine.getBytes());
                        i ++;
                    }
                    fos.flush();
                    fos.close();
                    return true;
                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    return false;
                }

            default:
                return false;
        }
    }
    
    /**
     * 按行返回文件内容
     * @param file 文件
     * @return 文件内容
     */
    public List<String> readFrom(File file) {
    	
    	List<String> arrList = new ArrayList<String>();
    	if (file.exists()) {
    		try {
				InputStream is = new FileInputStream(file);
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				String str = "";
				while ((str = br.readLine()) != null) {
					arrList.add(str);
				}
			} catch (FileNotFoundException e) {
				System.out.println(e.getMessage());
			} catch (IOException e1) {
				System.out.println(e1.getMessage());
			}
    		return arrList;
    	} else 
    		return null;
    }
    
}
